package com.bawei.park2211a.news

import android.app.Activity
import android.content.Intent

import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route

import com.bawei.base.bean.NewsEntity
import com.bawei.base.mvvm.BaseActivity

import com.bawei.park2211a.databinding.ActivityAddNewsBinding
import com.bigkoo.pickerview.builder.TimePickerBuilder

import com.bumptech.glide.Glide
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/app/news/AddNewsActivity")
class AddNewsActivity : BaseActivity<ActivityAddNewsBinding,AddNewsViewModel>() {
    lateinit var newsEntity: NewsEntity
    override fun initView() {
        newsEntity= NewsEntity()
        binding.addNewsIcon.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        binding.addNewsDate.setOnClickListener {
            TimePickerBuilder(this) { date, _ ->
                val sdf= SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                binding.addNewsDate.text=sdf.format(date)
                newsEntity.publishDate=sdf.format(date)

                Log.d("TGA", ""+SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(date)))
            }.build().show()
        }
        val arrayListOf = arrayListOf("园区新闻", "园区公告")
        val typeAdapter: ArrayAdapter<String> = ArrayAdapter(this,
            com.chad.library.R.layout.support_simple_spinner_dropdown_item,arrayListOf)
        binding.addNewsType.adapter=typeAdapter
        binding.addNewsType.onItemSelectedListener =object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                        newsEntity.type=arrayListOf[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }
        binding.addnewsSubmit.setOnClickListener {
            newsEntity.approved=true
            newsEntity.published=true
            newsEntity.content=binding.addNewsDetail.text.toString()
            newsEntity.title=binding.addNewsTitle.text.toString()
            newsEntity.publisher=binding.addNewsUsername.text.toString()
            viewModel.addNews(newsEntity)
        }

    }

    override fun initData() {
        viewModel.addNewsSuccess.observe(this){
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== Activity.RESULT_OK){
            val parcelable = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            Glide.with(this).load(parcelable?.path).into(binding.addNewsIcon)
        }
    }
}